package com.laoxiang.modules.system.controller;



import com.laoxiang.modules.system.entity.SysMenu;
import com.laoxiang.modules.system.entity.dto.RoleMenuTreeSelectDTO;
import com.laoxiang.modules.system.entity.vo.MenuTreeVo;
import com.laoxiang.modules.system.service.SysMenuService;
import com.laoxiang.utils.AppHttpCodeEnum;
import com.laoxiang.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 菜单权限表(SysMenu)表控制层
 *
 * @author db
 * @since 2023-07-26 08:59:09
 */
@RestController
@RequestMapping("/sysMenu")
@Api(tags = "菜单管理")
public class SysMenuController  {
    /**
     * 服务对象
     */
    @Resource
    private SysMenuService sysMenuService;

    @ApiOperation(httpMethod = "GET", value = "角色分配菜单时菜单列表")
    @GetMapping("/treeSelect")
    public ResponseResult treeSelect(){
        List<MenuTreeVo> menuTreeVoList = sysMenuService.treeSelect();
        return ResponseResult.okResult(menuTreeVoList);
    }

    @ApiOperation(httpMethod = "GET", value = "加载角色时显示的已分配的菜单和全部菜单")
    @GetMapping("/roleMenuTreeSelect/{roleId}")
    public ResponseResult roleMenuTreeSelect(@PathVariable("roleId") Long roleId){
        RoleMenuTreeSelectDTO roleMenuTreeSelectDTO = sysMenuService.roleMenuTreeSelect(roleId);
        return ResponseResult.okResult(roleMenuTreeSelectDTO);
    }


    @ApiOperation(httpMethod = "GET", value = "菜单管理界面菜单列表")
    @GetMapping("/getTreeMenuList")
    public ResponseResult getTreeMenuList(SysMenu sysMenu){
        List<SysMenu> treeMenuList = sysMenuService.getTreeMenuList(sysMenu);
        System.out.println(treeMenuList.toString());
        return ResponseResult.okResult(treeMenuList);
    }
    
    @ApiOperation(httpMethod = "POST",value = "增加菜单")
    @PostMapping("/addMenu")
    public ResponseResult addMenu(@ApiParam(name = "菜单信息",required = true)@RequestBody SysMenu sysMenu){
        int count = sysMenuService.addMenu(sysMenu);
        if(count>0){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.MENU_INSERT.getCode(),AppHttpCodeEnum.MENU_INSERT.getMsg());
        }
    }

    @ApiOperation(httpMethod = "GET",value = "根据菜单ID获得菜单信息")
    @GetMapping("/getMenuById/{menuId}")
    public ResponseResult getMenuById(@PathVariable("menuId") Long menuId){
        SysMenu menu = sysMenuService.getMenuById(menuId);
        return ResponseResult.okResult(menu);
    }

    @ApiOperation(httpMethod = "PUT",value = "修改菜单")
    @PutMapping("/updateMenu")
    public ResponseResult updateMenu(@ApiParam(name = "菜单信息",required = true)@RequestBody SysMenu sysMenu){
        sysMenuService.updateMenu(sysMenu);
        return ResponseResult.okResult();
    }

    @ApiOperation(httpMethod = "DELETE",value = "删除菜单")
    @DeleteMapping("/deleteMenuById/{menuId}")
    public ResponseResult deleteMenuById(@PathVariable("menuId") Long menuId){
        sysMenuService.deleteMenuById(menuId);
        return ResponseResult.okResult();
    }

}

